package com.ruoyi.system.service.msg.enricher;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DesignBase;
import com.ruoyi.system.domain.DesignBasicInformationGen;
import com.ruoyi.system.domain.DesignScheme;
import com.ruoyi.system.domain.DesignSchemeImage;
import com.ruoyi.system.dto.kujiale.PageResponse;
import com.ruoyi.system.dto.kujiale.info.RenderingImage;
import com.ruoyi.system.dto.kujiale.pano.RoamItem;
import com.ruoyi.system.entity.MsgPushTaskDO;
import com.ruoyi.system.enums.MsgTypeEnum;
import com.ruoyi.system.enums.SpaceTypeEnum;
import com.ruoyi.system.mapper.DesignBasicInformationGenMapper;
import com.ruoyi.system.mapper.DesignSchemeImageMapper;
import com.ruoyi.system.mapper.DesignSchemeMapper;
import com.ruoyi.system.service.DesignBaseService;
import com.ruoyi.system.service.DesignSchemeImageService;
import com.ruoyi.system.service.DesignSchemeService;
import com.ruoyi.system.service.KuJiaLeService;
import com.ruoyi.system.service.msg.MsgDetailEnricher;
import com.ruoyi.system.util.MsgAttachmentConverter;
import com.ruoyi.system.utils.PageConvertUtil;
import com.ruoyi.system.utils.SysUserUtils;
import com.ruoyi.system.vo.H5MsgDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 设计资料消息详情丰富器
 * 用于丰富设计资料类型消息的详情信息，包括设计方案基础信息、设计方案列表、设计图片等
 *
 * @author ruoyi
 * @since 2024-01-01
 */
@Component
@Slf4j
public class DesignDataMsgEnricher implements MsgDetailEnricher {

    @Resource
    private DesignSchemeService designSchemeService;

    @Autowired
    private KuJiaLeService kuJiaLeService;

    @Autowired
    private DesignBaseService designBaseService;

    @Override
    public boolean supports(Integer msgType) {
        return Objects.equals(msgType, MsgTypeEnum.DESIGN_DATA.getCode());
    }

    @Override
    public void enrich(H5MsgDetailVO vo, MsgPushTaskDO task) {
        if (task.getBusinessId() == null || task.getBusinessId().trim().isEmpty()) {
            return;
        }
        try {
            DesignScheme scheme = designSchemeService.getById(Long.parseLong(task.getBusinessId()));
            List<DesignSchemeImage> images = new ArrayList<>();
            if (scheme.getUseOnlineDesign() != null && scheme.getUseOnlineDesign().equals(1)
                    && StringUtils.isNotBlank(scheme.getOnlineDesignId())) {
                images.addAll(Objects.requireNonNull(getKujialeImg(scheme.getOnlineDesignId())));
                // 查询全屋漫游图
                // 调用服务层获取漫游列表
                DesignBase designBase = designBaseService.lambdaQuery().eq(DesignBase::getId, scheme.getDesignBaseId()).one();
                Long creatorId = designBase.getCreatorId();
                String kujialeIdBySysUserId = SysUserUtils.getKujialeIdBySysUserId(creatorId);
                if (StringUtils.isNotBlank(kujialeIdBySysUserId)) {
                    List<RoamItem> roamList = kuJiaLeService.getRoamList(kujialeIdBySysUserId, scheme.getOnlineDesignId());
                    if (CollectionUtil.isNotEmpty(roamList)) {
                        List<DesignSchemeImage> roamListResp = getDesignSchemeImages(roamList);
                        images.addAll(roamListResp);
                    }
                }
            }
            vo.setDesignSchemeImageList(images);
        } catch (Exception e) {
            // 记录日志但不影响主流程
            log.error("设计资料消息详情丰富器处理失败，业务ID：{}", task.getBusinessId(), e);
        }
    }



    private List<DesignSchemeImage> getKujialeImg(String kujialeDesignId) {
        PageConvertUtil.PageParam convert = PageConvertUtil.convert(1, 50);
        PageResponse<RenderingImage> pageResponse = kuJiaLeService.renderpicList(kujialeDesignId, convert.getStart(), convert.getNum());
        List<RenderingImage> result = pageResponse.getResult();
        if (CollectionUtil.isEmpty(result)) {
            return null;
        }
        List<DesignSchemeImage> resultList = Lists.newArrayList();
        for (RenderingImage renderingImage : result) {
            DesignSchemeImage designSchemeImage = new DesignSchemeImage();
            resultList.add(designSchemeImage);

            if ( StringUtils.isBlank(renderingImage.getImg())) {
                continue;
            }
            designSchemeImage.setFilePath(renderingImage.getImg());
            SpaceTypeEnum byLabel = SpaceTypeEnum.getByLabel(renderingImage.getRoomName());
            if (byLabel != null) {
                designSchemeImage.setSpaceName(byLabel.getLabel());
                designSchemeImage.setSpaceType(byLabel.getValue());
            } else {
                designSchemeImage.setSpaceName(StringUtils.isBlank(renderingImage.getRoomName()) || renderingImage.getRoomName().equals("未知") ? SpaceTypeEnum.OTHER.getLabel() : renderingImage.getRoomName());
                designSchemeImage.setSpaceType(SpaceTypeEnum.OTHER.getValue());
            }

        }
        return resultList;

    }

    /**
     * 酷家乐全屋漫游图转换为images
     */
    private static List<DesignSchemeImage> getDesignSchemeImages(List<RoamItem> roamList) {
        List<DesignSchemeImage> roamListResp = Lists.newArrayList();
        for (RoamItem roamItem : roamList) {
            DesignSchemeImage designSchemeImage = new DesignSchemeImage();
            designSchemeImage.setSpaceType("roamList");
            designSchemeImage.setSpaceName(roamItem.getPanoType().equals(1) ? "普通漫游图" : "智能漫游图");
            designSchemeImage.setFilePath(roamItem.getCoverUrl());
            designSchemeImage.setFileName(roamItem.getRoamUrl());
            roamListResp.add(designSchemeImage);
        }
        return roamListResp;
    }
}
